package org.ams.common.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ams.common.entity.SystemMoniterCache;
import org.ams.common.exception.SystemException;
import org.ams.common.util.Common;
import org.ams.log.entity.LoginLog;
import org.ams.log.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Scope("prototype")
@RequestMapping(value = "/monitor/")
public class MonitorController {

	@Autowired
	private LoginLogService loginLogService;
	
	@RequestMapping(value = "welcome", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String welcome(HttpServletRequest request, Model model) {
		try{
			request.removeAttribute("error");
			LoginLog loginLog = loginLogService.queryLastLoginLogByAccountId(Common.getloginAccountId());
			Integer loginCount = loginLogService.queryLoginCountByAccountId(Common.getloginAccountId());
			model.addAttribute("loginIp", loginLog.getLoginIp());
			model.addAttribute("loginTime", loginLog.getLoginTime());
			model.addAttribute("loginCount", loginCount);
			model.addAttribute(SystemMoniterCache.OS_NAME, SystemMoniterCache.systemMonitorCache.get(SystemMoniterCache.OS_NAME));
			model.addAttribute(SystemMoniterCache.HOST_IP, SystemMoniterCache.systemMonitorCache.get(SystemMoniterCache.HOST_IP));
			model.addAttribute(SystemMoniterCache.MEMORY_USED_RATE, SystemMoniterCache.systemMonitorCache.get(SystemMoniterCache.MEMORY_USED_RATE));
			model.addAttribute(SystemMoniterCache.JVM_USED_RATE, SystemMoniterCache.systemMonitorCache.get(SystemMoniterCache.JVM_USED_RATE));
			return "welcome";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	
	@RequestMapping(value = "data", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> data(HttpServletRequest request, Model model) {
		return SystemMoniterCache.systemMonitorCache; 
	}
	
	
	
}
